home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / tcpdump-3.0 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  6.7 KB  |  358 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] =
  24.     "@(#) $Header: util.c,v 1.28 94/06/12 14:30:31 leres Exp $ (LBL)";
  25. #endif
  26.  
  27. #include <sys/types.h>
  28. #include <sys/time.h>
  29. #include <sys/file.h>
  30. #include <sys/stat.h>
  31.  
  32. #include <ctype.h>
  33. #ifdef SOLARIS
  34. #include <fcntl.h>
  35. #endif
  36. #ifdef __STDC__
  37. #include <stdlib.h>
  38. #endif
  39. #include <stdio.h>
  40. #if __STDC__
  41. #include <stdarg.h>
  42. #else
  43. #include <varargs.h>
  44. #endif
  45. #include <string.h>
  46. #include <unistd.h>
  47.  
  48. #include "interface.h"
  49.  
  50. /*
  51.  * Print out a filename (or other ascii string).
  52.  * If ep is NULL, assume no truncation check is needed.
  53.  * Return true if truncated.
  54.  */
  55. int
  56. fn_print(register const u_char *s, register const u_char *ep)
  57. {
  58.     register int ret;
  59.     register u_char c;
  60.  
  61.     ret = 1;            /* assume truncated */
  62.     putchar('"');
  63.     while (ep == NULL || s < ep) {
  64.         c = *s++;
  65.         if (c == '\0') {
  66.             ret = 0;
  67.             break;
  68.         }
  69.         if (!isascii(c)) {
  70.             c = toascii(c);
  71.             putchar('M');
  72.             putchar('-');
  73.         }
  74.         if (!isprint(c)) {
  75.             c ^= 0x40;    /* DEL to ?, others to alpha */
  76.             putchar('^');
  77.         }
  78.         putchar(c);
  79.     }
  80.     putchar('"');
  81.     return(ret);
  82. }
  83.  
  84. /*
  85.  * Print out a counted filename (or other ascii string).
  86.  * If ep is NULL, assume no truncation check is needed.
  87.  * Return true if truncated.
  88.  */
  89. int
  90. fn_printn(register const u_char *s, register u_int n,
  91.       register const u_char *ep)
  92. {
  93.     register int ret;
  94.     register u_char c;
  95.  
  96.     ret = 1;            /* assume truncated */
  97.     putchar('"');
  98.     while (ep == NULL || s < ep) {
  99.         if (n-- <= 0) {
  100.             ret = 0;
  101.             break;
  102.         }
  103.         c = *s++;
  104.         if (!isascii(c)) {
  105.             c = toascii(c);
  106.             putchar('M');
  107.             putchar('-');
  108.         }
  109.         if (!isprint(c)) {
  110.             c ^= 0x40;    /* DEL to ?, others to alpha */
  111.             putchar('^');
  112.         }
  113.         putchar(c);
  114.     }
  115.     putchar('"');
  116.     return(ret);
  117. }
  118.  
  119. /*
  120.  * Print the timestamp
  121.  */
  122. void
  123. ts_print(register const struct timeval *tvp)
  124. {
  125.     register int s;
  126.     extern int32 thiszone;
  127.  
  128.     if (tflag > 0) {
  129.         /* Default */
  130.         s = (tvp->tv_sec + thiszone) % 86400;
  131.         (void)printf("%02d:%02d:%02d.%06d ",
  132.             s / 3600, (s % 3600) / 60, s % 60, tvp->tv_usec);
  133.     } else if (tflag < 0) {
  134.         /* Unix timeval style */
  135.         (void)printf("%d.%06d ", tvp->tv_sec, tvp->tv_usec);
  136.     }
  137. }
  138.  
  139. int
  140. data_print(register u_char *data, register int len)
  141. {
  142.     register int i, j=0;
  143.     u_char buffer[8192];
  144.  
  145.     for ( i=0; i<len; ++i, ++data ) {
  146.         if ( *data < ' ' ) {
  147.             buffer[j++]='^';
  148.             buffer[j++]=(*data + '@');
  149.         } else if ( *data > (char)127 )
  150.             buffer[j++]='.';
  151.         else if ( *data == (char)127 ) {
  152.             buffer[j++]='^';
  153.             buffer[j++]='?';
  154.         } else
  155.             buffer[j++]=(*data);
  156.     }
  157.     buffer[j] = '\0';
  158.  
  159.     return fwrite(buffer, j, 1, stdout);
  160. }
  161.  
  162. /*
  163.  * Convert a token value to a string; use "fmt" if not found.
  164.  */
  165. const char *
  166. tok2str(register const struct token *lp, register const char *fmt,
  167.     register int v)
  168. {
  169.     static char buf[128];
  170.  
  171.     while (lp->s != NULL) {
  172.         if (lp->v == v)
  173.             return (lp->s);
  174.         ++lp;
  175.     }
  176.     if (fmt == NULL)
  177.         fmt = "#%d";
  178.     (void)sprintf(buf, fmt, v);
  179.     return (buf);
  180. }
  181.  
  182. /* A replacement for strdup() that cuts down on malloc() overhead */
  183. char *
  184. savestr(register const char *str)
  185. {
  186.     register u_int size;
  187.     register char *p;
  188.     static char *strptr = NULL;
  189.     static u_int strsize = 0;
  190.  
  191.     size = strlen(str) + 1;
  192.     if (size > strsize) {
  193.         strsize = 1024;
  194.         if (strsize < size)
  195.             strsize = size;
  196.         strptr = malloc(strsize);
  197.         if (strptr == NULL)
  198.             error("savestr: malloc");
  199.     }
  200.     (void)strcpy(strptr, str);
  201.     p = strptr;
  202.     strptr += size;
  203.     strsize -= size;
  204.     return (p);
  205. }
  206.  
  207. #ifdef NOVFPRINTF
  208. /*
  209.  * Stock 4.3 doesn't have vfprintf.
  210.  * This routine is due to Chris Torek.
  211.  */
  212. vfprintf(f, fmt, args)
  213.     FILE *f;
  214.     char *fmt;
  215.     va_list args;
  216. {
  217.     int ret;
  218.  
  219.     if ((f->_flag & _IOWRT) == 0) {
  220.         if (f->_flag & _IORW)
  221.             f->_flag |= _IOWRT;
  222.         else
  223.             return EOF;
  224.     }
  225.     ret = _doprnt(fmt, args, f);
  226.     return ferror(f) ? EOF : ret;
  227. }
  228. #endif
  229.  
  230. /* VARARGS */
  231. __dead void
  232. #if __STDC__ || defined(SOLARIS)
  233. error(char *fmt, ...)
  234. #else
  235. error(fmt, va_alist)
  236.     char *fmt;
  237.     va_dcl
  238. #endif
  239. {
  240.     va_list ap;
  241.  
  242.     (void)fprintf(stderr, "%s: ", program_name);
  243. #if __STDC__
  244.     va_start(ap, fmt);
  245. #else
  246.     va_start(ap);
  247. #endif
  248.     (void)vfprintf(stderr, fmt, ap);
  249.     va_end(ap);
  250.     if (*fmt) {
  251.         fmt += strlen(fmt);
  252.         if (fmt[-1] != '\n')
  253.             (void)fputc('\n', stderr);
  254.     }
  255.     exit(1);
  256.     /* NOTREACHED */
  257. }
  258.  
  259. /* VARARGS */
  260. void
  261. #if __STDC__ || defined(SOLARIS)
  262. warning(char *fmt, ...)
  263. #else
  264. warning(fmt, va_alist)
  265.     char *fmt;
  266.     va_dcl
  267. #endif
  268. {
  269.     va_list ap;
  270.  
  271.     (void)fprintf(stderr, "%s: warning: ", program_name);
  272. #if __STDC__
  273.     va_start(ap, fmt);
  274. #else
  275.     va_start(ap);
  276. #endif
  277.     (void)vfprintf(stderr, fmt, ap);
  278.     va_end(ap);
  279.     if (*fmt) {
  280.         fmt += strlen(fmt);
  281.         if (fmt[-1] != '\n')
  282.             (void)fputc('\n', stderr);
  283.     }
  284. }
  285.  
  286. /*
  287.  * Copy arg vector into a new buffer, concatenating arguments with spaces.
  288.  */
  289. char *
  290. copy_argv(register char **argv)
  291. {
  292.     register char **p;
  293.     register int len = 0;
  294.     char *buf;
  295.     char *src, *dst;
  296.  
  297.     p = argv;
  298.     if (*p == 0)
  299.         return 0;
  300.  
  301.     while (*p)
  302.         len += strlen(*p++) + 1;
  303.  
  304.     buf = malloc(len);
  305.  
  306.     p = argv;
  307.     dst = buf;
  308.     while ((src = *p++) != NULL) {
  309.         while ((*dst++ = *src++) != '\0')
  310.             ;
  311.         dst[-1] = ' ';
  312.     }
  313.     dst[-1] = '\0';
  314.  
  315.     return buf;
  316. }
  317.  
  318. char *
  319. read_infile(char *fname)
  320. {
  321.     struct stat buf;
  322.     int fd;
  323.     char *p;
  324.  
  325.     fd = open(fname, O_RDONLY);
  326.     if (fd < 0)
  327.         error("can't open '%s'", fname);
  328.  
  329.     if (fstat(fd, &buf) < 0)
  330.         error("can't state '%s'", fname);
  331.  
  332.     p = malloc((u_int)buf.st_size);
  333.     if (read(fd, p, (int)buf.st_size) != buf.st_size)
  334.         error("problem reading '%s'", fname);
  335.  
  336.     return p;
  337. }
  338.  
  339. int
  340. gmt2local()
  341. {
  342. #ifndef SOLARIS
  343.     struct timeval now;
  344.     struct timezone tz;
  345.     long t;
  346.  
  347.     if (gettimeofday(&now, &tz) < 0)
  348.         error("gettimeofday");
  349.     t = tz.tz_minuteswest * -60;
  350.     if (localtime((time_t *)&now.tv_sec)->tm_isdst)
  351.         t += 3600;
  352.     return (t);
  353. #else
  354.     tzset();
  355.     return (-altzone);
  356. #endif
  357. }
  358.